<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-30 11:19:00
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-10 16:57:42
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
function getallmulu_qula($url, $startzhangjiekey = 0)
{
    if(!preg_match("/^https?:\/\/www\.qu\.la\/book\/\d+\/?\s*?$/ism",$url)){
        echo '<h3>'.lang('plugin/jameson_caiji','mulubuzhengque').'https://www.qu.la/book/60952/</h3>';exit;
    }
    global $_G;
    global $dqcode;
    preg_match('/^(.*?:\/\/.*?)\//i',$url,$dqcode);
    $articles                    = $data                    = array();
    $count                       = $zhangjienum                       = 0;
    preg_match("/\d+_(\d+)\/?/i",$url,$imgids);
    $articles[$count]['j_title'] = $articles[$count]['j_title'] = lang('plugin/jameson_caiji', 'diyijuan');
    $data                        = QueryList::Query($url, array(
        "href"      => array('#list dd a[style]', 'href'),
        "text"      => array('#list dd a[style]', 'text'),
        "book_name" => array('#info h1', 'text'),
        "img"       => array('img[src^="/BookFiles"]','src'),
        "author"    => array('#info h1+p', 'text'),
    ),'',CHARSET,'utf-8')->getData(function ($x) {
        global $dqcode;
        $x['author'] = preg_replace("/^.*?".lang('plugin/jameson_caiji','maohao')."/i","",$x['author']);
        $x['href'] = $dqcode[1] . $x['href'];
        return $x;
    });
    // debug($data);
    @dmkdir('./data/attachment/forum/jamesoncaiji');
    if($data[0]['img']){
        $articles[$count]['image'] = 'jamesoncaiji/qula-'.TIMESTAMP.'.jpg';
        file_put_contents("./data/attachment/forum/".$articles[$count]['image'],file_get_contents($dqcode[1].'/'.$data[0]['img']));
        if(!file_exists("./data/attachment/forum/".$articles[$count]['image']) || filesize("./data/attachment/forum/".$articles[$count]['image'])<10){
            file_put_contents("./data/attachment/forum/".$articles[$count]['image'],file_get_contents('https://www.wenxuemi.com/modules/article/images/nocover.jpg'));
        }
    }
    $articles[$count]['bookname'] = $data[0]['book_name'];
    $articles[$count]['author'] = $data[0]['author']?$data[0]['author']:$_G['jameson_caiji']['author'];
    unset($data[0]['book_name']);
    unset($data[0]['author']);
    $lastid = count($data);
    if ($startzhangjiekey) {
        // 如果是计划任务中，则删掉已有的
        array_splice($data, 0, $startzhangjiekey);
    }
    $articles[$count]['zhangjie'] = $data;
    $articles[1]['lastid']        = $lastid;
    return $articles;
}
